<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="数据日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.refDate`" :rules="formRules.refDate" class="mb-0px!">
            <el-date-picker
              v-model="row.refDate"
              :readonly="formType == 'detail'"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="mainData.dataTypeCode == 4 || mainData.dataTypeCode == 6" label="数据小时" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.refHour`" :rules="formRules.refHour" class="mb-0px!">
            <el-select
              :disabled="formType == 'detail'"
              v-model="row.refHour"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in refHourOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="mainData.dataTypeCode == 1" label="msgid" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.msgid`" :rules="formRules.msgid" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.msgid" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="mainData.dataTypeCode == 1" label="标题" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.title`" :rules="formRules.title" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.title" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="mainData.dataTypeCode != 5 && mainData.dataTypeCode != 6" label="图文页（点击群发图文卡片进入的页面）阅读人数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.intPageReadUser`" :rules="formRules.intPageReadUser" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.intPageReadUser" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="mainData.dataTypeCode != 5 && mainData.dataTypeCode != 6" label="图文页阅读次数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.intPageReadCount`" :rules="formRules.intPageReadCount" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.intPageReadCount" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="mainData.dataTypeCode != 5 && mainData.dataTypeCode != 6" label="原文页（点击图文页“阅读原文”进入的页面）阅读人数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.oriPageReadUser`" :rules="formRules.oriPageReadUser" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.oriPageReadUser" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="mainData.dataTypeCode != 5 && mainData.dataTypeCode != 6" label="原文页阅读次数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.oriPageReadCount`" :rules="formRules.oriPageReadCount" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.oriPageReadCount" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="mainData.dataTypeCode == 5 || mainData.dataTypeCode == 6" label="分享场景" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.shareScene`" :rules="formRules.shareScene" class="mb-0px!">
            <el-select
              :disabled="formType == 'detail'"
              v-model="row.shareScene"
              clearable
              filterable
              class="!w-240px"
            >
              <el-option
                v-for="item in shareSceneOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="分享人数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.shareUser`" :rules="formRules.shareUser" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.shareUser" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="分享次数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.shareCount`" :rules="formRules.shareCount" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.shareCount" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="mainData.dataTypeCode != 5 && mainData.dataTypeCode != 6" label="收藏人数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.addToFavUser`" :rules="formRules.addToFavUser" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.addToFavUser" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="mainData.dataTypeCode != 5 && mainData.dataTypeCode != 6" label="收藏次数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.addToFavCount`" :rules="formRules.addToFavCount" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.addToFavCount" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="mainData.dataTypeCode != 5 && mainData.dataTypeCode != 6" label="传播渠道" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.userSource`" :rules="formRules.userSource" class="mb-0px!">
            <el-select
              :disabled="formType == 'detail'"
              v-model="row.userSource"
              clearable
              filterable
              class="!w-240px"
            >
              <el-option
                v-for="item in userSourceOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="formType != 'detail'" align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row v-if="formType != 'detail'" justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加微信文章分析子表(按渠道区分)</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { WeixinArticleAnalysisApi } from '@/api/integration/weixinarticleanalysis'

const props = defineProps<{
  analysisId: undefined, // 分析表数据主键（主表的关联字段）
  formType: undefined, // 操作类型
  mainData: any // 主表数据
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

const refHourOptions = [
  {
    value: 0,
    label: '1时',
  },
  {
    value: 100,
    label: '2时',
  },
  {
    value: 200,
    label: '3时',
  },
  {
    value: 300,
    label: '4时',
  },
  {
    value: 400,
    label: '5时',
  },
  {
    value: 500,
    label: '6时',
  },
  {
    value: 600,
    label: '7时',
  },
  {
    value: 700,
    label: '8时',
  },
  {
    value: 800,
    label: '9时',
  },
  {
    value: 900,
    label: '10时',
  },
  {
    value: 1000,
    label: '11时',
  },
  {
    value: 1100,
    label: '12时',
  },
  {
    value: 1200,
    label: '13时',
  },
  {
    value: 1300,
    label: '14时',
  },
  {
    value: 1400,
    label: '15时',
  },
  {
    value: 1500,
    label: '16时',
  },
  {
    value: 1600,
    label: '17时',
  },
  {
    value: 1700,
    label: '18时',
  },
  {
    value: 1800,
    label: '19时',
  },
  {
    value: 1900,
    label: '20时',
  },
  {
    value: 2000,
    label: '21时',
  },
  {
    value: 2100,
    label: '22时',
  },
  {
    value: 2200,
    label: '23时',
  },
  {
    value: 2300,
    label: '24时',
  }
]

const shareSceneOptions = [
  {
    value: 1,
    label: '好友转发',
  },
  {
    value: 2,
    label: '朋友圈',
  },
  {
    value: 255,
    label: '其他',
  }
]

const userSourceOptions = [
  {
    value: 0,
    label: '会话',
  },
  {
    value: 1,
    label: '好友',
  },
  {
    value: 2,
    label: '朋友圈',
  },
  {
    value: 4,
    label: '历史消息页',
  },
  {
    value: 5,
    label: '其他',
  },
  {
    value: 6,
    label: '看一看',
  },
  {
    value: 7,
    label: '搜一搜',
  }
]

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.analysisId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await WeixinArticleAnalysisApi.getWeixinArticleAnalysisSourceListByAnalysisId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    analysisId: undefined,
    refDate: props.mainData.refDate,
    refHour: props.mainData.refHour,
    msgid: undefined,
    title: undefined,
    intPageReadUser: undefined,
    intPageReadCount: undefined,
    oriPageReadUser: undefined,
    oriPageReadCount: undefined,
    shareScene: undefined,
    shareUser: undefined,
    shareCount: undefined,
    addToFavUser: undefined,
    addToFavCount: undefined,
    userSource: undefined,
    dataTypeCode: props.mainData.dataTypeCode,
    dataType: props.mainData.dataType
  }
  row.analysisId = props.analysisId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>
